﻿using MESPro.Controls;
using MESPro.Enums;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;

namespace MESPro.Views
{
    public partial class ToolBoxFrm : DockContent
    {
        //private const int SB_HORZ = 0;
        //private const int SB_VERT = 1;
        //private const int SB_CTL = 2;
        //private const int SB_BOTH = 3;


        //[DllImport("user32", EntryPoint = "ShowScrollBar")]
        //public static extern int ShowScrollBar(int hwnd, int wBar, int bShow);
        public ToolBoxFrm()
        {
            InitializeComponent();
        
            this.Load += ToolBoxFrm_Load;
        }
        /// <summary>
        /// ListView选择项改变后触发
        /// </summary>
        public Action<ListViewItem?>? RMListViewItemSelected { get; set; }
        /// <summary>
        /// 提交到数据库后,需要刷新界面
        /// </summary>
        public Action? RMUpdateableByObject { get; set; }
        /// <summary>
        /// 发生异常后触发的动作
        /// </summary>
        public Action<object, MsgLevel>? RMShowMsg { get; set; }
        private void ToolBoxFrm_Load(object sender, EventArgs e)
        {
            //this.listView1.Scrollable = false;
            //ShowScrollBar((int)this.listView1.Handle, SB_VERT, 1);
            var typeList = Assembly.GetExecutingAssembly().GetTypes().Where(x => x.GetInterfaces().Contains(typeof(ICustomControl))).ToList();
            this.listView1.Items.Clear();
           // ListViewGroup g1 = new ListViewGroup(); //创建一个组
           // ListViewGroup g2 = new ListViewGroup(); //创建一个组
            
            foreach (var item in typeList)
            {
                var des = item.GetCustomAttribute<RMControlAttribute>().Name;
                var type = item.GetCustomAttribute<RMControlAttribute>().Type;
                var imagekey = item.GetCustomAttribute<RMControlAttribute>().ImageKey;
                ListViewItem listViewItem1 = new ListViewItem(des, imagekey);
                listViewItem1.Tag = item;
                if (type == "容器控件")
                {
                    listViewItem1.Group = this.listView1.Groups[1];
                }
                else
                {
                    listViewItem1.Group = this.listView1.Groups[0];
                }
                this.listView1.Items.Add(listViewItem1);
            }

        }

        private void listView1_ItemDrag(object sender, ItemDragEventArgs e)
        {
            ListView? src = sender as ListView;
            if (src != null)
            {
                src.DoDragDrop(e.Item, DragDropEffects.Move);
            }
        }

        private void listView1_DragEnter(object sender, DragEventArgs e)
        {
            e.Effect = DragDropEffects.Move;
        }
    }
}
